import { RootState } from "@/store"
import { RouteMeta, RouteRecordRaw } from "vue-router"
import { Module } from "vuex"
import { IFrontHeaderActions, IFrontHeaderGetter, IFrontHeaderMutations, IFrontHeaderState } from "./types"
const state: IFrontHeaderState = {
    logo: require('@/assets/img/logo.png'),
    title: '实验室监控平台',
    subTitle: 'Laboratory monitoring platform',
    routes: [
        // 动态数据
    ],
    performance: {
        time: '2020-01-01 00:00:00',
        CPU: '0%',
        Mem: '0%'
    }
}

const actions: IFrontHeaderActions = {

}

const mutations: IFrontHeaderMutations = {
     // 一级路由生成导航栏
    _generateRoutes(state: IFrontHeaderState, { routes, role }: { routes: RouteRecordRaw[], role: string }) {
       if(routes.length === 0) return;
        // 将路由中的一级路由根据权限过滤后生成导航栏
        routes.forEach((route: RouteRecordRaw) => {
            // 判断路由表中是否已存在该路由
            const isExist = state.routes.some((item) => item.path === route.path);
            if (route.meta && route.meta.roles && !isExist) {
                const meta = route.meta as RouteMeta;
                if (meta.roles.includes(role)) {
                    const o = {};
                    o['id'] = new Date().getTime();
                    o['path'] = route.path;
                    Object.keys(meta).forEach((key) => o[key] = meta[key]);
                    state.routes.push(o)
                }
            }
        })
    },

}
const getters: IFrontHeaderGetter = {

}

export const frontHeader: Module<IFrontHeaderState, RootState> = {
    namespaced: true,
    state,
    actions,
    mutations,
    getters,
}
